function [PQ, status, scheduledTargets] = SCP_ScheduleRun(PQ, s1TargetList, s2TargetList)
%IA_ScheduleRun_SCP Simple Constraint Programming
% Detailed explanation goes here

scheduledTargets = [];
status = 0;
randomTargetList = randperm(PQ_Size(PQ));

for priority = 1:PQ_Priorities(PQ)
  for target = 1:length(randomTargetList)
    [Task, status] = PQ_Fetch_Task(PQ, randomTargetList(target), PQ_Priorities(PQ)+1-priority);
    if ~status
      continue;
    end
    
    target_id = Task.id;
    consistent = 1;
    
    for node = 1:length(scheduledTargets)
      node_id = scheduledTargets(node);
      if node_id ~= target_id
        [t_row1, ~] = ind2sub(size(s1TargetList), find(s1TargetList == target_id));
        [n_row1, ~] = ind2sub(size(s1TargetList), find(s1TargetList == node_id));

        if n_row1 < t_row1
          relation = IA_Relation_Less();
        elseif n_row1 == t_row1
          relation = IA_Relation_Dummy();
        else
          relation = IA_Relation_Greater();
        end

        [t_row2, ~] = ind2sub(size(s2TargetList), find(s2TargetList == target_id));
        [n_row2, ~] = ind2sub(size(s2TargetList), find(s2TargetList == node_id));

        if n_row2 < t_row2
          relation = IA_Relation_Intersect(relation, IA_Relation_Less());
        elseif n_row2 == t_row2
          relation = IA_Relation_Intersect(relation, IA_Relation_Dummy());
        else
          relation = IA_Relation_Intersect(relation, IA_Relation_Greater());
        end

        if IA_Relation_Is_Null(relation)
          consistent = 0;
          break;
        end
      end
    end
    
    if consistent
      scheduledTargets = [scheduledTargets target_id]; %#ok<AGROW>
    end
  end
end

% Sort the scheduled targets by consulting the target lists per sensor
for node1 = 1:size(scheduledTargets, 2)-1
  for node2 = node1+1:size(scheduledTargets, 2)
    [s1Row1] = ST_FindTargetId2(s1TargetList, scheduledTargets(node1));
    [s1Row2] = ST_FindTargetId2(s1TargetList, scheduledTargets(node2));
    
    [s2Row1] = ST_FindTargetId2(s2TargetList, scheduledTargets(node1));
    [s2Row2] = ST_FindTargetId2(s2TargetList, scheduledTargets(node2));
    
    if (s1Row2 < s1Row1) || (s2Row2 < s2Row1)
      temp = scheduledTargets(node1);
      scheduledTargets(node1) = scheduledTargets(node2); %#ok<AGROW>
      scheduledTargets(node2) = temp; %#ok<AGROW>
    end
  end
end

if ~isempty(scheduledTargets)
  status = 1;
end
end %function